<template>
  <view class="page-container">
    <nav-bar title="发布商品" textAlign="center"></nav-bar>
    <!-- 标题 -->
    <view class="title-container">
      <input type="text" v-model="articleTitle" placeholder="输入一个醒目的标题~" maxlength="40" />
    </view>
    <!-- 文章内容 -->
    <view class="content-container">
      <textarea class="textarea-view" v-model="articleContent" :data-maxnum="articleContent.length+'/500'"
        placeholder="尽量描述一下自己的宝贝~" :maxlength="500" />
    </view>
    <view class="tips"> 默认第一张为主图哦~ </view>
    <!-- 图片区域 -->
    <image-upload @change="handleUploadChange" />
    <!-- 选择标签区域 -->
    <view class="topic-container">
      <view class="title">
        <text>添加话题</text>
      </view>
      <view class="list">
        <view :class="familyId===item.familyId? 'clazx' : 'uplabel_text'" v-for="(item,index) in label"
          :key="item.familyId" @click="handleSelectTopic(item)">
          {{item.familyName}}
        </view>
      </view>
    </view>

    <view class="release-contaner">
      <view class="left">
        商品金额
      </view>
      <!-- 金额输入框 -->
      <view class="right">
        <input placeholder-style="color:#3FD3D1" class="ipt" type="digit" placeholder="￥ 00000.00" maxlength="6"
          @input="handleToMoney" v-model="money" />
      </view>
    </view>
    <view class="release_button" @click="handleReleaseGoods">
      <text>{{send_loading?'发布中':'发布'}}</text>
    </view>
  </view>
</template>

<script>
  import {
    getIntactGoodsTopicApi,
    postUpoloadApi
  } from '@/api/used/publish-product.js'

  function trimStr(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
  }
  export default {

    data() {
      return {
        // 文章标题
        articleTitle: "",
        // 文章内容
        articleContent: "",
        // 标签
        label: [],
        // 标签ID
        familyId: '',
        // 图片列表
        imgpath: [],
        // 价格
        money: '',
        status: true,
        send_loading: false
        // percent:0
      };
    },
    onLoad() {
      this.send_loading = false;
      this.handleGetIntactGoodsTopic()
    },
    methods: {
      // 上传状态发生了变化
      handleUploadChange(e) {
        this.imgpath = e.data;
        this.status = e.status;
      },
      // 金额转换
      handleToMoney(e) {
        let that = this;
        let price = e.detail.value
        if (price.indexOf(".") == 0) {
          //'首位小数点情况'
          price = price.replace(/[^$#$]/g, "0.");
          price = price.replace(/\.{2,}/g, ".");
        }
        price = price.match(/^\d*(\.?\d{0,2})/g)[0] || null;
        //重新赋值给input
        this.$nextTick(() => {
          this.money = price;
        });

      },
      // 查询分类列表
      async handleGetIntactGoodsTopic() {
        const {
          data: res
        } = await getIntactGoodsTopicApi()
        if (res.success) {
          this.label = res.data.list
        }
      },
      // 选择标签
      handleSelectTopic(item) {
        this.familyId = item.familyId;
      },
      // 添加文章
      async handleReleaseGoods() {
        if (!this.status) {
          uni.showToast({
            title: '图片正在上传中，暂不能发布',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        // 验证
        if (trimStr(this.articleTitle) == '' || trimStr(this.articleContent) == '') {
          uni.showToast({
            title: '标题或描述不能为空',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.imgpath.length == 0) {
          uni.showToast({
            title: '图片不能为空',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.familyId == '') {
          uni.showToast({
            title: '请选择发布标签',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.money == '' || this.money == null) {
          uni.showToast({
            title: '请输入商品价格',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.articleTitle.length > 16) {
          uni.showToast({
            title: '标题需要在16字以内',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.articleContent.length > 500) {
          uni.showToast({
            title: '内容需要在500字以内',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        if (this.send_loading) {
          uni.showToast({
            title: '正在发布中',
            icon: 'none',
            duration: 2000
          });
          return
        }
        this.send_loading = true;
        uni.showLoading({
          title: '正在发布中',
        });
        let {
          data: res
        } = await postUpoloadApi(this.familyId, this.articleTitle, this.articleContent, this.imgpath,
          this.money);
        console.log(res);
        this.send_loading = false;
        uni.hideLoading()
        if (res.success) {
          uni.showToast({
            title: '物品发布成功',
            duration: 2000
          })
          uni.$emit('goods-update', {})
          uni.navigateBack();
        } else {
          uni.showToast({
            title: res.message,
            duration: 2000,
            icon: 'error'
          })
        }
      },

    },
  }
</script>

<style lang="scss">
  .page-container {
    background-color: #FFFFFF;
    box-sizing: border-box;
    padding: 32rpx;

    input {
      width: 100%;
      height: 100rpx;
      background-color: #FFFFFF;
      font-size: 40rpx;
    }

    .title-container {}

    .content-container {
      border-top: 2rpx solid #AEAEAE;
      background-color: #FFFFFF;
      font-size: 32rpx;
      padding-top: 20rpx;

      .textarea-view {
        border-radius: 5px;
        width: calc(100% - 16px);
        padding: 16rpx 16rpx 40rpx 16rpx;
        min-height: 120rpx;
        line-height: 40rpx;
        color: #000000;
        position: relative;
        height: 400rpx;
        border-bottom: #AEAEAE solid 2rpx;

        &:after {
          content: attr(data-maxnum);
          position: absolute;
          right: 10rpx;
          bottom: 0rpx;
          font-size: 28rpx;
          color: gray;
        }
      }

    }


    .tips {
      color: #AEAEAE;
      font-size: 26rpx;
      width: 100%;
      padding: 20rpx 0;
    }

    .topic-container {
      .title {
        font-size: 34rpx;
        width: 100%;
        margin-top: 20rpx;
        font-weight: bold;
      }

      .list {
        width: 100%;
        margin-bottom: 50rpx;
        min-height: 100rpx;
        font-size: 24rpx;
        color: #FFFFFF;

        .uplabel_text {
          margin-top: 30rpx;
          float: left;
          margin-right: 20rpx;
          background-color: #cccccc;
          max-width: 140rpx;
          border-radius: 32rpx;
          text-align: center;
          padding: 8rpx 12rpx;
          width: 160rpx;
          height: 44rpx;
          line-height: 44rpx;
        }

        .clazx {
          margin-top: 30rpx;
          float: left;
          margin-right: 20rpx;
          background-color: #3FD3D1;
          max-width: 140rpx;
          border-radius: 32rpx;
          text-align: center;
          padding: 8rpx 12rpx;
          width: 160rpx;
          height: 44rpx;
          line-height: 44rpx;
        }
      }


    }


    .release-contaner {
      width: 100%;
      height: 200rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .left {
        font-size: 34rpx;
        font-weight: bold;
      }

      .right {
        max-width: 230rpx;
        height: 62rpx;
        color: #3FD3D1;
        margin-right: 30rpx;

        input {
          border-radius: 46rpx;
          background-color: #F6F6F6;
          min-width: 180rpx;
          max-width: 230rpx;
          height: 64rpx;
          line-height: 64rpx;
          color: #3FD3D1;
          font-size: 32rpx;
          padding: 0rpx 10rpx;
          text-align: center;
        }
      }


    }

    .release_button {
      background-color: #3fd3d1;
      color: #FFFFFF;
      width: 100%;
      height: 110rpx;
      line-height: 120rpx;
      border-radius: 16rpx;
      font-size: 36rpx;
      text-align: center;
      margin: 20rpx auto;
    }
  }
</style>